package com.psa.dialog;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import com.psa.element.*;
import com.psa.system.*;
import com.psa.tool.SystemStatus;
import com.wxw.BaseLibrary.adapter.ObjectListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by david on 2014-06-09.
 */
public class SalesTypeDialog extends DialogManager {
    private Spinner salesType_spinner;
    private Button btnOK;
    private List<SalesType> salesTypes = new ArrayList<SalesType>();
    private SalesType currentSalesType;
    private boolean canListener = false;
    private OnSalesTypeSelectedListener onSalesTypeSelectedListener;

    public void setOnSalesTypeSelectedListener(OnSalesTypeSelectedListener onSalesTypeSelectedListener) {
        this.onSalesTypeSelectedListener = onSalesTypeSelectedListener;
    }

    public SalesTypeDialog(Activity activity){
        parentActivity = activity;
        initView();
    }

    public void setCurrentSalesType(String salesType) {
        int i = 0;
        for (SalesType st : salesTypes) {
            if (st.TypeName.equals(salesType)) {
                salesType_spinner.setSelection(i);
                break;
            }
            i++;
        }
    }

    @Override
    public View initView() {
        layout = parentActivity.getLayoutInflater().inflate(R.layout.salestype_spinner, null);
        salesType_spinner = (Spinner)layout.findViewById(R.id.sp_sales_type);
        salesTypes.add(new SalesType(SalesType.CashSales, SalesType.getName(SalesType.CashSales)));
        salesTypes.add(new SalesType(SalesType.TradeOffer, SalesType.getName(SalesType.TradeOffer)));
        salesTypes.add(new SalesType(SalesType.Sampling, SalesType.getName(SalesType.Sampling)));
        salesTypes.add(new SalesType(SalesType.Damage, SalesType.getName(SalesType.Damage)));
        salesTypes.add(new SalesType(SalesType.Expiration, SalesType.getName(SalesType.Expiration)));
        salesTypes.add(new SalesType(SalesType.Replacement, SalesType.getName(SalesType.Replacement)));
        salesTypes.add(new SalesType(SalesType.Shortage, SalesType.getName(SalesType.Shortage)));
        ObjectListAdapter<SalesType> adapter = new ObjectListAdapter<SalesType>(parentActivity, salesTypes);
        adapter.setHasBorder(true);
        adapter.setFontSize(20);
        salesType_spinner.setAdapter(adapter);
        salesType_spinner.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                canListener = true;
                return false;
            }
        });
        salesType_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                currentSalesType = salesTypes.get(i);
                if (canListener) {
                    if (onSalesTypeSelectedListener != null)
                        onSalesTypeSelectedListener.onSalesTypeSelected(SalesTypeDialog.this, currentSalesType);
                    canListener = false;
                    dialog.dismiss();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        btnOK = (Button)layout.findViewById(R.id.btnOK);
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onSalesTypeSelectedListener!=null) {
                    onSalesTypeSelectedListener.onSalesTypeSelected(SalesTypeDialog.this, currentSalesType);
                }
                dialog.dismiss();
            }
        });
        title = SystemStatus.MainActivity.getString(R.string.salestype_input_title);
        tv_title = (TextView)layout.findViewById(R.id.tv_title);
        return layout;
    }

    public interface OnSalesTypeSelectedListener{
        void onSalesTypeSelected(SalesTypeDialog dialog, SalesType salesType);
    }
}
